package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.RepaireStatus;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.OnlineRepair;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.FailRepairReqVO;
import com.smfx.yxt.service.IOnlineRepairService;
import com.smfx.yxt.vo.FailRepairQueryVO;

/**
 * 故障报修
 * 000 待维修
 * 001 已维修
 *维修人员职位：7011
 * */
@Controller
@RequestMapping(Const.TEACHER_ROLE + MenuConst.FAIL_REPAIRE)
public class FailRepaireController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(FailRepaireController.class);
	
	@Autowired
	private IOnlineRepairService onlineRepairService;
	
	@RequestMapping(value = {"","/"})
	private String list(Model model) {
		model.addAttribute("isRepaire", onlineRepairService.isRepairMan(getAppUser().getTeacher().getName()));
		return "failRepair/list";
	}
	
	@RequestMapping("detail/{repairId}")
	private String detail(Model model, @PathVariable Integer repairId) {
		model.addAttribute("isRepaire", onlineRepairService.isRepairMan(getAppUser().getTeacher().getName()));
		model.addAttribute("detail", onlineRepairService.selectDetailView(repairId));
		return "failRepair/detail";
	}
	
	@RequestMapping("add")
	private String add() {
		return "failRepair/add";
	}
	
	@RequestMapping("addRepair/submit")
	@ResponseBody Object addRepaire(FailRepairReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setRepairId(appUser.getTeacher().getId());
			reqVO.setRepairName(appUser.getTeacher().getName());
			reqVO.setRepairPhone(appUser.getTeacher().getMobile());
			reqVO.setRepaireStatus(RepaireStatus.REPAIREING.getCode());
			reqVO.setRepairTime(new Date());
			reqVO.setTermId(getCurrentTerm().getId());
			onlineRepairService.addRepaire(reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----addRepaire error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("reply/submit")
	@ResponseBody Object repaireReply(FailRepairReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			int count = onlineRepairService.isRepairMan(getAppUser().getTeacher().getName());
			if(count == 0) {
				return new RspResult(Status.NO_REPAIRE);
			}
			reqVO.setRepaireId(appUser.getTeacher().getId());
			reqVO.setRepaireName(appUser.getTeacher().getName());
			reqVO.setRepairePhone(appUser.getTeacher().getMobile());
			reqVO.setRepaireStatus(RepaireStatus.REPAIRED.getCode());
			reqVO.setReplyTime(new Date());
			onlineRepairService.updateRepaire(reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----repaireReply error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(FailRepairQueryVO vo) {
		try {
			if(vo.getRepaireId() != null) {
				vo.setRepairer(getAppUser().getTeacher().getName());
			}
			Page<OnlineRepair> page = new Page<OnlineRepair>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("报修时间", "desc"));
			sorts.add(new OrderBy("回复时间", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OnlineRepair> list = onlineRepairService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("repairType")
	@ResponseBody Object repaireType() {
		return onlineRepairService.selectRepaireTypeList();
	}
	
	@RequestMapping("repairBuilding")
	@ResponseBody Object repairBuilding() {
		return onlineRepairService.selectRepairBuildingList();
	}
	
}
